package org.southyin.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.southyin.commons.CommonsResp;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter("/login4")
public class FilterC_Login4 implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if (username != null && !"".equals(username.trim()) && password != null && !"".equals(password.trim())) {
            filterChain.doFilter(req, resp);
        }else{
            CommonsResp failed = CommonsResp.failed("参数校验不合法");
            failed.setCode(406);
            resp.getWriter().println(new ObjectMapper().writeValueAsString(failed));
        }
    }

    public void destroy() {

    }
}
